package cn.wenhe9.yygh.user.service.impl;

import cn.wenhe9.yygh.cmn.client.DictFeignClient;
import cn.wenhe9.yygh.common.result.ResultResponse;
import cn.wenhe9.yygh.enums.DictEnum;
import cn.wenhe9.yygh.model.user.Patient;
import cn.wenhe9.yygh.user.mapper.PatientMapper;
import cn.wenhe9.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author DuJinliang
 * 2022/4/6
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Resource
    private DictFeignClient dictFeignClient;

    @Override
    public ResultResponse findAllByUserId(Long userId) {

        //根据userid查询所有就诊人的信息列表
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Patient> patientList = baseMapper.selectList(queryWrapper);
        //通过远程调用,得到编码对应的内容 查询数据字典表中的内容
        patientList.forEach(item -> {
            this.packPatient(item);
        });

        return ResultResponse.ok(patientList);
    }

    @Override
    public Patient getPatientById(Long id) {
        Patient patient = baseMapper.selectById(id);
        //封装数据
        packPatient(patient);
        return patient;
    }

    /**
     * patient对象其他参数的封装
     * @param patient  Patient
     */
    private void packPatient(Patient patient){
        //根据证件类型编码，获取证件类型具体值
        String certificatesTypeString =
                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        //联系人证件类型
        String contactsCertificatesTypeString =
                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactsCertificatesType());
        //省
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());
        //市
        String cityString = dictFeignClient.getName(patient.getCityCode());
        //区
        String districtString = dictFeignClient.getName(patient.getDistrictCode());

        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
    }
}
